<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  businessTripAdd,
  businessTripFind,
  businessTripFindById,
  businessTripModify,
  businessTripRemove
} from '@/api/businessTrip/businessTrip'
import { Message, Modal } from '@arco-design/web-vue'
import { useGlobalStore } from '@/stores/global'
import { log } from 'node:util'
import router from '@/router'

const globalStore = useGlobalStore()

// 搜索表单
const searchForm = reactive({
  // 显示控制
  visible: true,
  // 表单
  form: {
    name: null,  //出差名称
    startTime: null  //开始时间
  },
  // 搜索
  search: () => {
    businessTripFind(searchForm.form,
      searchForm.list.pageSize,
      searchForm.list.pageNum
    ).then((resp: any) => {
      searchForm.list = resp.data
    })
  },
  // 重置
  reset: () => {
    searchForm.form = {
      name: null,  //出差名称
      startTime: null  //开始时间
    }
  },
  // table 数据
  list: {
    list: [],
    total: 0,
    pageSize: 10,
    pageNum: 1
  }
})

// 操作数据表单
const operationForm = reactive({
  // 弹框标题
  title: '添加出差',
  // 是否显示
  visible: false,
  // 表单
  form: {
    id: null,  //id
    name: null,  //出差名称
    addr: null,  //出差地点
    startTime: null,  //开始时间
    endTime: null,  //结束时间
    remark: null  //备注
  },
  // 验证
  rules: {
    name: [{ required: true, message: '出差名称不能为空' }],
    startTime: [{ required: true, message: '开始时间不能为空' }]
  },
  reset: () => {
    operationForm.form = {
      id: null,  //id
      name: null,  //出差名称
      addr: null,  //出差地点
      startTime: null,  //开始时间
      endTime: null,  //结束时间
      remark: null  //备注
    }
  },
  // 打开弹框
  open: () => {
    operationForm.title = '添加出差'
    operationForm.visible = true
    operationForm.reset()
  },
  modify: (record: any) => {
    operationForm.title = '修改出差'
    businessTripFindById(record.id).then(resp => {
      operationForm.form = resp.data
      operationForm.visible = true
    })
  },
  remove: (record: any) => {
    Modal.confirm({
      title: '警告！',
      content: `是否确认删除 [${record.id}] 出差？`,
      onOk(e) {
        businessTripRemove(record.id).then(() => {
          searchForm.search()
        })
      }
    })
  },
  submit: ({ errors, values }: any) => {
    if (errors) return
    if (values.id) {
      businessTripModify(operationForm.form).then(() => {
        operationForm.visible = false
        searchForm.search()
      })
      return
    }

    businessTripAdd(values).then(() => {
      operationForm.visible = false
      searchForm.search()
    })
  }
})

onMounted(() => {
  searchForm.search()
  document.title = 'Rocc管理系统 出差管理'
})

</script>

<template>
  <div class="w-full h-full">

    <!-- 搜索表单 -->
    <a-form v-show="searchForm.visible">
      <a-row :gutter="10">
        <a-col :sm="12" :md="6">
          <a-form-item label="出差名称">
            <a-input v-model="searchForm.form.name" />
          </a-form-item>
        </a-col>
        <a-col :sm="12" :md="6">
          <a-form-item label="开始时间">
            <a-input v-model="searchForm.form.startTime" />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-space>
            <a-button @click="searchForm.search">
              <template #icon>
                <icon-search />
              </template>
              搜索
            </a-button>
            <a-button @click="searchForm.reset">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-form>
    <!-- 搜索表单end -->


    <!-- 功能区域 -->
    <div class="flex justify-between mr-1">
      <a-space wrap>
        <a-button type="primary" @click="operationForm.open">
          <template #icon>
            <icon-plus />
          </template>
          添加出差
        </a-button>
        <a-button>
          <template #icon>
            <icon-download />
          </template>
          导出出差
        </a-button>
      </a-space>
      <a-space>
        <a-tooltip content="刷新列表" @click="searchForm.search" position="tr">
          <a-button size="small" shape="circle">
            <template #icon>
              <icon-refresh />
            </template>
          </a-button>
        </a-tooltip>

        <a-tooltip content="隐藏搜索" position="tr" @click="searchForm.visible = !searchForm.visible">
          <a-button size="small" shape="circle">
            <template #icon>
              <icon-search />
            </template>
          </a-button>
        </a-tooltip>

      </a-space>
      <!-- 功能区域end -->
    </div>

    <!-- 表单区域 -->
    <a-drawer v-model:visible="operationForm.visible" :title="operationForm.title" :width="500" :footer="false">
      <a-form ref="operationFormRef" :model="operationForm.form" @submit="operationForm.submit"
              :rules="operationForm.rules">
        <a-form-item hide-asterisk label="出差名称" field="name" validate-trigger="blur">
          <a-input v-model="operationForm.form.name" />
        </a-form-item>

        <a-form-item hide-asterisk label="出差地点" field="addr" validate-trigger="blur">
          <a-input v-model="operationForm.form.addr" />
        </a-form-item>

        <a-form-item hide-asterisk label="开始时间" field="startTime" validate-trigger="blur">
          <a-date-picker show-time format="YYYY-MM-DD HH:mm:ss" v-model="operationForm.form.startTime" />
        </a-form-item>

        <a-form-item hide-asterisk label="结束时间" field="endTime" validate-trigger="blur">
          <a-date-picker show-time format="YYYY-MM-DD HH:mm:ss" v-model="operationForm.form.endTime" />
        </a-form-item>

        <a-form-item hide-asterisk label="备注" field="remark" validate-trigger="blur">
          <a-textarea v-model="operationForm.form.remark" :auto-size="{minRows: 15,maxRows: 15}" />
        </a-form-item>

        <a-form-item>
          <a-button type="primary" html-type="submit">
            提交
          </a-button>
        </a-form-item>
      </a-form>
    </a-drawer>

    <a-table :data="searchForm.list.list" :pagination="false"
             @cell-click="record => router.push({path: '/businessTripExpense',query:{id:record.id}})">
      <template #columns>
        <a-table-column title="id" data-index="id" width="80" />
        <a-table-column title="出差名称" data-index="name" ellipsis />
        <a-table-column title="出差地点" data-index="addr" />
        <a-table-column title="开始时间" data-index="startTime" width="180" />
        <a-table-column title="结束时间" data-index="endTime" width="180" />
        <!--        <a-table-column title="备注" data-index="remark" />-->
        <a-table-column title="操作" width="240">
          <template #cell="{record}">
            <a-space>
              <a-button status="warning" @click.stop="operationForm.modify(record)">
                <template #icon>
                  <icon-edit />
                </template>
                修改
              </a-button>
              <a-button status="danger" @click.stop="operationForm.remove(record)">
                <template #icon>
                  <icon-delete />
                </template>
                删除
              </a-button>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>
    <div class="mt-4 flex justify-end">
      <a-pagination v-model:current="searchForm.list.pageNum"
                    v-model:page-size="searchForm.list.pageSize"
                    :total="searchForm.list.total"
                    @change="searchForm.search"
                    show-total
                    show-jumper
                    show-page-size
      ></a-pagination>
    </div>
  </div>
</template>

<style scoped>

</style>
